<?php
/**
 * Yjnic_Sae_Logger 直接输出日记到控制台的日记类 
 * 
 * - 测试环境下使用
 * 
 * @package     Sae\Logger
 * @author      prettyyjnic <prettyyjnic@qq.com> 2015-12-27
 */

class Yjnic_Sae_Logger extends PhalApi_Logger {
    private $_kv_ins = null;

    public function __construct($level){
        parent::__construct($level);
        $this->_kv_ins = new SaeKV();
        $this->_kv_ins->init();
    }

    public function log($type, $msg, $data) {
        $msg_arr = array();
        $msg_arr[] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
        $msg_arr[] = strtoupper($type);
        $msg_arr[] = str_replace(PHP_EOL, '\n', $msg);
        if ($data !== NULL) {
            $msg_arr[] = is_array($data) ? json_encode($data) : $data;
        }

        $content = implode('|', $msg_arr) . PHP_EOL;
        $key = 'log'.date('YmdHi', $_SERVER['REQUEST_TIME']);//用分钟作为最小粒度作为kv存储的key
        $value = $this->_kv_ins->get($key);
        if (!empty($value)) {
            $content = $value.PHP_EOL.$content;
        }
        return $this->_kv_ins->set($type, $content);
    }
}
